﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AMS.DeDuplicator.CommonLayer.Composite
{
    public class FileComponent : IFileComponent
    {
        string _fileName;
        string _directory;
        IDirectoryComposite _parentDirectory;

        public string FullName
        {
            get { return String.Format("{0}\\{1}", _parentDirectory.Path, _fileName); }
        }

        public string ParentDirectory
        {
            get { return _directory; }
        }

        public string Name
        {
            get { return _fileName; }
        }

        public long SizeInBytes
        {
            get { return new FileInfo(FullName).Length; }
        }

        public FileComponent(IDirectoryComposite directory, string fileName)
        {
            InputValidator.ValidateNotNull(directory);
            InputValidator.ValidateNotNull(fileName);
            InputValidator.ValidateDirectoryExists(directory.Path);
            InputValidator.ValidateFileExists(directory.Path + "\\" + fileName);

            _parentDirectory = directory;
            _directory = _parentDirectory.Path;
            _fileName = fileName;
        }

        public FileStream GetFileStream()
        {
            return new FileInfo(FullName).Open(FileMode.Open);
        }

        //// standalone FileComponent (no
        //public FileComponent(string directory, string fileName)
        //{
        //    InputValidator.ValidateNotNull(directory);
        //    InputValidator.ValidateNotNull(fileName);

        //}

        //public FileComponent(string fullName)
        //{

        //}

        public static long GetSizeInBytes(string filePath)
        {
            InputValidator.ValidateNotNull(filePath);
            InputValidator.ValidateFileExists(filePath);
            return new FileInfo(filePath).Length;
        }

        public static FileStream GetFileStream(string filePath)
        {
            InputValidator.ValidateNotNull(filePath);
            InputValidator.ValidateFileExists(filePath);
            return new FileInfo(filePath).Open(FileMode.Open);
        }
    }
}
